#include "Gate.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_EventManager.h"
#include "../SGD Wrappers/SGD_AudioManager.h"
#include "GameplayState.h"

Gate::Gate()
{
	m_hImage = SGD::GraphicsManager::GetInstance()->LoadTexture("resource/graphics/Gate.png");
	SGD::EventManager::GetInstance()->RegisterForEvent(this, "GateDrop");
	SGD::EventManager::GetInstance()->RegisterForEvent(this, "GateRaise");
	m_bActive = false;
	m_fDistance;

	m_aAudio = SGD::AudioManager::GetInstance()->LoadAudio("resource/audio/Gate_Close.wav");

	m_bPlayed = true;
	m_bGateUp = true;
}

Gate::~Gate()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_hImage);
	SGD::AudioManager::GetInstance()->UnloadAudio(m_aAudio);
}

void Gate::Update(float dt)
{
	if (m_bActive)
	{
		m_ptPosition.y += 850 * dt;
		if (m_ptPosition.y >= m_ptStartPos.y + (16 * 32))
		{
			m_ptPosition.y = m_ptStartPos.y + (16 * 32);
			m_bActive = false;
		}
	}
	if (m_bGateUp && !m_bActive)
	{
		m_ptPosition.y -= 850 * dt;
		if (m_ptPosition.y <= m_ptStartPos.y - (16 * 32))
		{
			m_ptPosition.y = m_ptStartPos.y - (16 * 32);
			m_bGateUp = false;
		}
	}
}

void Gate::Render()
{
	GameplayState* game = GameplayState::GetInstance();
	SGD::Point pos(m_ptPosition.x - game->GetCamX(), m_ptPosition.y - game->GetCamY());

	SGD::GraphicsManager::GetInstance()->DrawTexture(m_hImage,pos);
}

void Gate::HandleEvent(const SGD::Event* pEvent)
{
	if (pEvent->GetEventID() == "GateDrop")
	{
		m_bActive = true;


		if (!SGD::AudioManager::GetInstance()->IsAudioPlaying(m_aAudio) && m_bPlayed)
			SGD::AudioManager::GetInstance()->PlayAudio(m_aAudio);
	}
	if (pEvent->GetEventID() == "GateRaise")
	{
		//m_ptPosition = m_ptStartPos;
		m_bGateUp = true;
		m_bPlayed = false;
	}
}